package com.lagou.homework.service.impl;

import com.lagou.homework.dao.CreateCodeDao;
import com.lagou.homework.pojo.AuthCode;
import com.lagou.homework.service.CreateCodeService;
import com.lagou.homework.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

@Service
public class CreateCodeServiceImpl implements CreateCodeService {

    @Autowired
    EmailService emailService;
    @Autowired
    AuthCode authCode;
    @Autowired
    CreateCodeDao createCodeDao;

    @Override
    public boolean create(String email) {
        // 获取随机的六位数字
        String code = String.valueOf((int)((Math.random()*9+1)*100000));
        // 调用发送邮件的微服务
        emailService.sendEmail(email, code);
        // 将验证码信息插入数据库
        authCode.setId(0);
        authCode.setEmail(email);
        authCode.setCode(code);
        Timestamp createtime = new Timestamp(System.currentTimeMillis()+ 8*60*60*1000);
        authCode.setCreatetime(createtime);
        Timestamp expiretime = new Timestamp(System.currentTimeMillis()+ 8*60*60*1000 +10*60*1000);
        authCode.setExpiretime(expiretime);
        createCodeDao.save(authCode);
        return true;
    }
}
